<template>
  <div>
   <!-- <div class="infoChar" ref="chartRef"></div> -->
    <div class="app-container">
      <div class="head-container">
        <el-input v-model="userName" clearable placeholder="输入学员名称搜索" maxlength="10" style="width: 200px;"
                  class="filter-item" @keyup.enter.native="init"/>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
      </div>
      <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" border>
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="60">
        </el-table-column>
        <el-table-column prop="taskName" label="任务名称" align="center" />
        <el-table-column prop="userName" label="人员名称" align="center" />
        <el-table-column prop="duration" label="任务时长" width="250" align="center" >
          <template slot-scope="scope">
            <span>{{formatSeconds(scope.row.duration)}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="finishDuration" label="已学习时长" width="250" align="center" >
          <template slot-scope="scope">
            <span>{{formatSeconds(scope.row.finishDuration)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否完成" width="150" align="center" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.finishDuration < scope.row.duration" type="danger">未完成</el-tag>
            <el-tag v-else type="success">已完成</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        :current-page="page + 1"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"/>
    </div>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import { formatSeconds } from '@/utils/index'
  import echarts from 'echarts'

  export default {
    name: 'index',
    mixins: [initData],
    data() {
      return {
        userName: '',
        id: this.$route.query.id
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    watch: {
      data: function() {
        //this.initScheduleCharts()
      }
    },
    methods: {
      formatSeconds,
      beforeInit() {
        this.url = 'course/study-record/page-list'
        this.params = { page: this.page, size: this.size, userName: this.userName, taskId: this.id }
        return true
      },
      initScheduleCharts() {
        const xData = this.data.map(item => item.userName)
        const yData = this.data.map(item => item.finishDuration)
        const option = {
          title: {
            text: '学习时长',
            x: 'center',
            textStyle: {
              fontSize: 13
            }
          },
          color: ['#73c0de'],
          xAxis: {
            type: 'category',
            data: xData
          },
          yAxis: {
            type: 'value',
            show:false
          },
          series: [{
            data: yData,
            type: 'bar',
            barWidth: 40,
            showBackground: true,
            label: {
              show: true,
              position: 'top',
              color: 'black',
              formatter: function(val) {
                return formatSeconds(val.data)
              }
            }
          }]
        }
        this.chart = echarts.init(this.$refs.chartRef)
        this.chart.setOption(option)
      }
    }
  }
</script>
<style scoped>
  .infoChar {
    min-height: calc(30vh);
    background-color: #f8f9fc;
    border-radius: 10px;
    margin-bottom: 10px;
    border: 1px solid #eaeefb;
  }
</style>
